package org.krivtsun.jpref;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: kontiky
 * Date: 24.03.2007
 * Time: 22:26:09
 * To change this template use File | Settings | File Templates.
 */
public class ImageProvider {

    private static BufferedImage cards;
    private static int cardWidth, cardHeight;

    public static void initialize() {
        try {
            cards = ImageIO.read(ImageProvider.class.getResource("images/cards.png"));
            cardWidth = cards.getWidth() / (Rank.values().length + 1);
            cardHeight = cards.getHeight() / Suit.values().length;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage getCardImage(Card card) {
        int x = card.getRank().ordinal() * cardWidth;
        int y = card.getSuit().ordinal() * cardHeight;
        return cards.getSubimage(x, y, cardWidth, cardHeight);
    }

    public static BufferedImage getBackImage(int i) {
        int x = Rank.values().length * cardWidth;
        int y = i * cardHeight;
        return cards.getSubimage(x, y, cardWidth, cardHeight);
    }

    public static int getCardWidth() {
        return cardWidth;
    }

    public static int getCardHeight() {
        return cardHeight;
    }

    public static void main(String[] args) {
        try {
            BufferedImage cards = ImageIO.read(ImageProvider.class.getResource("images/cards.png"));
            int cardWidth = cards.getWidth() / (Rank.values().length + 1);
            int cardHeight = cards.getHeight() / Suit.values().length;
            Graphics g = cards.getGraphics();
            g.setColor(Color.black);

            for (int x = 0; x < Rank.values().length + 1; x++) {
                for (int y = 0; y < Suit.values().length; y++) {
                    g.drawRoundRect(x * cardWidth, y * cardHeight, cardWidth - 1, cardHeight - 1, 9, 9);
                }
            }
            ImageIO.write(cards, "png", new File("d:/out.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
